package com.fancy.learncenter.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.EditInfoActivity;
import com.fancy.learncenter.activity.GetResumeActivity;
import com.fancy.learncenter.activity.H5Activity;
import com.fancy.learncenter.activity.LoginActivity;
import com.fancy.learncenter.activity.MyCollectionActivity;
import com.fancy.learncenter.activity.MyFancyMoveActivity;
import com.fancy.learncenter.activity.MyFriendActivity;
import com.fancy.learncenter.activity.MyOrderActivity;
import com.fancy.learncenter.activity.MyReleaseActivity;
import com.fancy.learncenter.activity.PersonInfoActivity;
import com.fancy.learncenter.activity.PullResumActivity;
import com.fancy.learncenter.activity.ResumeActivity;
import com.fancy.learncenter.activity.SettingActivity;
import com.fancy.learncenter.activity.WalletActivity;
import com.fancy.learncenter.activity.YGTVipActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.PersonDetailDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.SPUtils;
import com.superservice.lya.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.fancy.learncenter.activity.MainActivity.MAIN_REQUEST_CODE;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyselfFragment extends Fragment {

    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.user_account)
    TextView userAccount;
    @Bind(R.id.iamge_user)
    SimpleDraweeView iamgeUser;
    @Bind(R.id.ygt_end)
    TextView ygtEnd;

    public MyselfFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_myself, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @OnClick({R.id.setting, R.id.edit, R.id.rl_login, R.id.iamge_user, R.id.guanzhu, R.id.my_wallet,
            R.id.baoming, R.id.pull_resume, R.id.help, R.id.shoucang, R.id.resume, R.id.shoudao, R.id.order, R.id.my_release,
            R.id.ygt_vip})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.rl_login:
                startActivity(new Intent(getActivity(), EditInfoActivity.class));
                break;
            case R.id.iamge_user:
                intent = new Intent(getActivity(), PersonInfoActivity.class);
                intent.putExtra("userId", "" + SPUtils.getUserID());
                startActivity(intent);
                break;
            case R.id.edit:
                startActivity(new Intent(getActivity(), EditInfoActivity.class));
                break;
            case R.id.baoming:
                startActivity(new Intent(getActivity(), MyFancyMoveActivity.class));
                break;
            case R.id.shoucang:
                startActivity(new Intent(getActivity(), MyCollectionActivity.class));
                break;
            case R.id.resume:
                startActivity(new Intent(getActivity(), ResumeActivity.class));
                break;
            case R.id.shoudao:
                startActivity(new Intent(getActivity(), GetResumeActivity.class));
                break;
            case R.id.pull_resume:
                startActivity(new Intent(getActivity(), PullResumActivity.class));
                break;
            case R.id.order:
                startActivity(new Intent(getActivity(), MyOrderActivity.class));
                break;
            case R.id.guanzhu:
                startActivity(new Intent(getActivity(), MyFriendActivity.class));
                break;
            case R.id.my_release:
                startActivity(new Intent(getActivity(), MyReleaseActivity.class));
                break;
            case R.id.my_wallet:
                startActivity(new Intent(getActivity(), WalletActivity.class));
                break;
            case R.id.ygt_vip:
                intent = new Intent(getActivity(), YGTVipActivity.class);
                intent.putExtra("buyTime", buyTime);
                intent.putExtra("endTime", endTime);
                startActivity(intent);
                break;
            case R.id.help:

                intent = new Intent(getActivity(), H5Activity.class);

                intent.putExtra(H5Activity.URL_KEY, "http://yigoutong.shweijue.com/wx/user/help" + "?fromapp_user_id=" + MyApplication.userId);

                getActivity().startActivity(intent);

                break;

        }
    }

    String buyTime = "";
    String endTime = "";

    private void getData() {
        Map map = new HashMap();
        HttpMehtod.getInstance().getPersonDetail(map, new IdeaObserver<BaseDataBean<PersonDetailDataBean>>(getActivity()) {

            @Override
            public void onSuccess(BaseDataBean<PersonDetailDataBean> personDetailDataBeanBaseDataBean) {

                if (personDetailDataBeanBaseDataBean.getData() == null) {
                    return;
                }

                SPUtils.putUserName(personDetailDataBeanBaseDataBean.getData().getNick_name());
                SPUtils.putUserMobile(personDetailDataBeanBaseDataBean.getData().getAccount());
                SPUtils.putUserIcon(personDetailDataBeanBaseDataBean.getData().getImgsrc());
                SPUtils.putUserBirthday(personDetailDataBeanBaseDataBean.getData().getBirthday());
                SPUtils.putUserSign(personDetailDataBeanBaseDataBean.getData().getSign());
                SPUtils.putUserSex(personDetailDataBeanBaseDataBean.getData().getSex());


                if (!TextUtils.isEmpty(personDetailDataBeanBaseDataBean.getData().getImgsrc())) {
                    iamgeUser.setImageURI(personDetailDataBeanBaseDataBean.getData().getImgsrc());
                }


                userName.setText(personDetailDataBeanBaseDataBean.getData().getNick_name());

                userAccount.setText(personDetailDataBeanBaseDataBean.getData().getAccount());


                if (personDetailDataBeanBaseDataBean.getData().getYgt() != null && !TextUtils.isEmpty(personDetailDataBeanBaseDataBean.getData().getYgt().getStatus())) {


                    if (!TextUtils.isEmpty(personDetailDataBeanBaseDataBean.getData().getYgt().getEnd_day())) {
                        ygtEnd.setText(personDetailDataBeanBaseDataBean.getData().getYgt().getEnd_day() + "到期");

                        endTime = personDetailDataBeanBaseDataBean.getData().getYgt().getEnd_day();
                    }

                    if (!TextUtils.isEmpty(personDetailDataBeanBaseDataBean.getData().getYgt().getBuy_day())) {
                        buyTime = personDetailDataBeanBaseDataBean.getData().getYgt().getBuy_day();
                    }

                } else {
                    ygtEnd.setText("请购买易沟通");

                }

            }

            @Override
            public void onFail(int errorCode) {
//                if (errorCode == LOGIN_CODE || errorCode == LOGIN_CODE2) {
//                    Intent intent = new Intent(getActivity(), LoginActivity.class);
//                    getActivity().startActivityForResult(intent, MAIN_REQUEST_CODE);
//                }
            }
        });
    }
}
